package com.ioftv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ioftv.entity.PlatformConfig;
import com.ioftv.mapper.PlatformConfigMapper;
import com.ioftv.service.PlatformConfigService;
import com.ioftv.service.base.BaseService;
import org.springframework.stereotype.Service;

/**
 * 平台配置Service实现
 */
@Service
public class PlatformConfigServiceImpl extends BaseService<PlatformConfigMapper, PlatformConfig> implements PlatformConfigService {

    @Override
    public String getConfigValue(String configKey, String defaultValue) {
        LambdaQueryWrapper<PlatformConfig> wrapper = createUserQueryWrapper();
        wrapper.eq(PlatformConfig::getConfigKey, configKey);
        wrapper.eq(PlatformConfig::getStatus, 1);
        PlatformConfig config = this.getOne(wrapper);
        return config != null ? config.getConfigValue() : defaultValue;
    }
    
    @Override
    public String getConfigValue(String configKey, String defaultValue, Long userId) {
        LambdaQueryWrapper<PlatformConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlatformConfig::getConfigKey, configKey);
        wrapper.eq(PlatformConfig::getStatus, 1);
        wrapper.eq(PlatformConfig::getUserId, userId);
        PlatformConfig config = this.getOne(wrapper);
        return config != null ? config.getConfigValue() : defaultValue;
    }

    @Override
    public boolean updateConfigValue(String configKey, String configValue) {
        LambdaQueryWrapper<PlatformConfig> wrapper = createUserQueryWrapper();
        wrapper.eq(PlatformConfig::getConfigKey, configKey);
        PlatformConfig config = this.getOne(wrapper);
        
        if (config != null) {
            config.setConfigValue(configValue);
            return this.updateById(config);
        } else {
            // 如果配置项不存在，创建新的配置项
            PlatformConfig newConfig = new PlatformConfig();
            newConfig.setConfigKey(configKey);
            newConfig.setConfigValue(configValue);
            newConfig.setConfigDesc("平台配置项");
            newConfig.setStatus(1);
            // 设置用户ID
            setEntityUserId(newConfig);
            return this.save(newConfig);
        }
    }
    
    @Override
    public boolean updateConfigValue(String configKey, String configValue, Long userId) {
        LambdaQueryWrapper<PlatformConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlatformConfig::getConfigKey, configKey);
        wrapper.eq(PlatformConfig::getUserId, userId);
        PlatformConfig config = this.getOne(wrapper);
        
        if (config != null) {
            config.setConfigValue(configValue);
            return this.updateById(config);
        } else {
            // 如果配置项不存在，创建新的配置项
            PlatformConfig newConfig = new PlatformConfig();
            newConfig.setConfigKey(configKey);
            newConfig.setConfigValue(configValue);
            newConfig.setConfigDesc("平台配置项");
            newConfig.setStatus(1);
            newConfig.setUserId(userId);
            return this.save(newConfig);
        }
    }

    @Override
    protected void addUserCondition(LambdaQueryWrapper<PlatformConfig> queryWrapper) {
        queryWrapper.eq(PlatformConfig::getUserId, getCurrentUserId());
    }

    @Override
    protected void setEntityUserId(PlatformConfig entity) {
        if (entity.getUserId() == null) {
            entity.setUserId(getCurrentUserId());
        }
    }
}